#include "MyString.h"

//无参构造
MyString::MyString():size(0),capacity(15)
{
    s = new char[capacity];
}

//带参构造
MyString::MyString(const char *str):capacity(15)
{
    size = strlen(str);
    capacity = size + 1 > 15 ? size + 1 : 15;
    s = new char[capacity];
    strcpy(s,str);
}


//拷贝构造
MyString::MyString(const MyString &str)
{
    size = str.size;
    capacity = str.capacity;
    s = new char[capacity];
    
    strcpy(s,str.s);
} 

//赋值运算符重载
MyString& MyString::operator=(const MyString &str)
{
    delete []s;
    size = str.size;
    capacity = str.capacity;
    s = new char[capacity];
    strcpy(s,str.s);
    return *this;
}

//析构
MyString::~MyString()
{
    delete []s;
}

//+=运算符重载
MyString &MyString::operator+=(const MyString &str)
{
    size += str.size;
    if(size + 1 > capacity)
        ReSize(size + 1);
    strcat(this->s,str.s);
    return *this;
}

MyString &MyString::operator+=(const char &c)
{   
    if(size + 1 > capacity)
        ReSize(capacity * 2);
    this->s[size++] = c;
    this->s[size] = '\0';
    return *this;
}

//+运算符重载
MyString MyString::operator+(const MyString &str)
{
   MyString result = *this;
   result += str;
   return result;
}


//-=运算符重载
MyString &MyString::operator-=(const MyString &str)
{
    char *dest = strstr(this->s,str.s);
    while(dest != nullptr)
    {
        char* back = dest + str.size;
        while (*back != '\0')
        {
            *dest++ = *back++;
        }
        *dest = '\0';
        this->size -= str.size;
        dest = strstr(this->s,str.s);
    }

    if(size + 1 < capacity / 2)
        ReSize(capacity / 2);
    return *this;
}

//-运算符重载
MyString MyString::operator-(const MyString &str)
{
    MyString result = *this;
   result -= str;
   return result;
}


//[]重载
char &MyString::operator[](int index)
{
    return this->s[index];
}

//==运算符重载
bool MyString::operator==(const MyString &str)
{
    return strcmp(s,str.s) == 0;
}

//!=运算符重载
bool MyString::operator!=(const MyString &str)
{
    return strcmp(s,str.s) != 0;
}

//&&运算符重载
bool MyString::operator&&(const MyString &str)
{
   return this->size != 0 && str.size != 0;
}

MyString::operator int()
{
    return atoi(this->s);
}

MyString::operator double()
{
    return atof(this->s);
}

//字符串切割
MyStringArray MyString::split(const MyString &delim)
{
    MyString temp = *this;
    MyStringArray result;
    char* s = strtok(temp.s,delim.s);
    while(s != nullptr)
    {
        result += s;
        s = strtok(nullptr,delim.s);
    }
    return result;
}

//扩容
void MyString::ReSize(int capacity)
{
    this->capacity = capacity;
    char *newS = new char[capacity];
    size = size > capacity - 1 ? capacity - 1 : size;
    strncpy(newS,s,size);
    delete []s;
    s = newS;
}

//输出函数重载
std::ostream &operator<<(std::ostream &os, const MyString &str)
{
    os<<str.s;
    return os;
}

//输入函数重载
std::istream &operator>>(std::istream &is, MyString &str)
{
    char c = 0;
    while((c = getchar()) != '\n' && c != ' ')
    {
        str += c;
    }
    return is;
}

//字符串数组无参构造
MyStringArray::MyStringArray():size(0),capacity(15)
{
    array = new MyString[capacity];
}


//拷贝构造
MyStringArray::MyStringArray(const MyStringArray &a)
{
    size = a.size;
    capacity = a.capacity;
    array = new MyString[capacity];
    for(int i = 0;i < size;i++)
        array[i] = a.array[i];
}

//赋值运算符重载
MyStringArray &MyStringArray::operator=(const MyStringArray &a)
{
    delete []array;
    size = a.size;
    capacity = a.capacity;
    array = new MyString[capacity];
    for(int i = 0;i < size;i++)
        array[i] = a.array[i];
    return *this;
}

//析构
MyStringArray::~MyStringArray()
{
    delete []array;
}

//+=运算符重载
MyStringArray &MyStringArray::operator+=(const MyString &str)
{
    if(size == capacity)
    {
        capacity *= 2;
        MyString *newS = new MyString[capacity];
        for (int i = 0; i < size; i++)
        {
            newS[i] = array[i];
        }
        delete []array;
        array = newS;
    }
    array[size++] = str;
    return *this;
}

MyStringArray &MyStringArray::operator+=(const char &str)
{
    if(size == capacity)
    {
        capacity *= 2;
        MyString *newS = new MyString[capacity];
        for (int i = 0; i < size; i++)
        {
            newS[i] = array[i];
        }
        delete []array;
        array = newS;
    }
    array[size++] += str;
    return *this;
}

MyStringArray &MyStringArray::operator+=(const MyStringArray &str)
{
    // if(size + str.size > capacity)
    // {
    //     ReSizeArray(size + str.size);
    //     for(int i = size;i < capacity;i++)
    //         array[i] = str.array[i];
    // }
    // else
    // {
    //     for(int i = size,j = 0;i < size + str.size && j < str.size;i++,j++)
    //         array[i] = str.array[j];
    // }
    // size += str.size;
    for(int i = 0;i < str.size;i++)
    {
        if(size == capacity)
            ReSizeArray(capacity * 2);
        array[size++] = str.array[i];
    }

    return *this;
}

//+运算符重载
MyStringArray MyStringArray::operator+(const MyString &str)
{
    MyStringArray result = *this;
    result += str;
    return result;
}

MyStringArray MyStringArray::operator+(const MyStringArray &str)
{
    MyStringArray result = *this;
    for(int i = 0;i < str.size;i++)
    {
        result += str.array[i];
    }
    return result;
}

//-=运算符重载
MyStringArray &MyStringArray::operator-=(const MyString &str)
{
    for(int i = 0;i < size;i++)
    {
        if(this->array[i] == str)
        {
            for(int j = i;j < size -1;j++)
            {
                this->array[j] = this->array[j+1];
            }
            i--;
            this->size--;
        }  
    }
    if(size + 1 < capacity / 2)
        ReSizeArray(capacity / 2);
    return *this;
}

MyStringArray &MyStringArray::operator-=(const MyStringArray &str)
{
    for(int i = 0;i < str.size;i++)
    {
        *this -= str.array[i];
    }
    if(size + 1 < capacity / 2)
        ReSizeArray(capacity / 2);
    return *this;
}

//-运算符重载
MyStringArray MyStringArray::operator-(const MyString &str)
{
    MyStringArray result = *this;
    result -= str;
    return result;
}

MyStringArray MyStringArray::operator-(const MyStringArray &str)
{
    MyStringArray result = *this;
    for(int i = 0;i < str.size;i++)
    {
        result -= str.array[i];
    }
    return result;
}

//==运算符重载
bool MyStringArray::operator==(const MyString &str)
{
    for(int i = 0;i < size;i++)
    {
        if(array[i] == str)
            return true;
    }
    return false;
}

bool MyStringArray::operator==(const MyStringArray &str)
{
    for(int i = 0;i < str.size;i++)
    {
        if(*this == str.array[i])
            return true;
    }
    return false;
}

//！=运算符重载
bool MyStringArray::operator!=(const MyString &str)
{
    for(int i = 0;i < size;i++)
    {
        if(array[i] == str)
            return false;
    }
    return true;

}

//[]重载
MyString &MyStringArray::operator[](int index)
{
   return this->array[index];
}

//扩容
void MyStringArray::ReSizeArray(int capacity)
{
    this->capacity = capacity;
    MyString *newS = new MyString[capacity];
    size = size > capacity - 1 ? capacity - 1 : size;
    for(int i = 0;i < size;i++)
        newS[i] = array[i];
    delete []array;
    array = newS;
}

//输出重载
std::ostream &operator<<(std::ostream &os, const MyStringArray &a)
{
    for(int i = 0;i < a.size;i++)
        os<<a.array[i]<<std::endl;
    return os;
}

//输入重载
std::istream &operator>>(std::istream &is, MyStringArray &a)
{
    MyString str;
    std::cin>>str;
    a.array[a.size++] = str;
    return is;
}
